﻿
Imports std

Public Class parameters_mapping(Of nT As _int64)
    Inherits x_separated_names
    Implements iParametersMapping

    Public Const parameterSeparator As Char = character.comma

    Private Shared _n As nT = Nothing

    Shared Sub New()
        _n = alloc(Of nT)()
    End Sub

    Private Shared Function n() As Int64
        Return +(_n)
    End Function

    Private Shared Function isParameterSeparator(ByVal c As Char) As Boolean
        Return c = parameterSeparator
    End Function

    Protected Friend Shared Function _isMeaningfulMark(ByVal c As Char) As Boolean
        Return isParameterSeparator(c)
    End Function

    Protected Sub New(ByVal v As vector(Of String))
        MyBase.New(v)
    End Sub

    Public Function iParametersMapping() As vector(Of String)
        Return +Me
    End Function

    Public Shared Function create(ByVal v As vector(Of String)) As parameters_mapping(Of nT)
        Dim _v As vector(Of String) = Nothing
        copy(_v, v)
        Return internalCreate(_v)
    End Function

    Private Shared Function internalCreate(ByVal v As vector(Of String)) As parameters_mapping(Of nT)
        Return New parameters_mapping(Of nT)(v)
    End Function

    Protected Overloads Shared Function parseImpl(ByVal input As String, ByRef index As Int64, _
                                                  ByRef output As parameters_mapping(Of nT)) As Boolean
        Return parseImpl(input, index, output, _
                         AddressOf isParameterSeparator, _
                         AddressOf value_name_defination.parse, _
                         AddressOf internalCreate)
    End Function

    Public Shared Function parse(ByVal input As String, ByRef index As Int64, _
                                 ByRef output As parameters_mapping(Of nT)) As Boolean
        Return parsebase(input, index, output, AddressOf parseImpl)
    End Function

    Default Public Shadows ReadOnly Property at(ByVal index As Int64) As String Implements iParametersMapping.at
        Get
            If index < n() Then
                Return MyBase.at(index)
            Else
                assert(MyBase.size() > n(), "mybase.size() <= n(), cannot use serial_mapping logic")
                Return MyBase.at((index - n()) Mod (MyBase.size() - n()) + n()) + _
                       Convert.ToString((index - n()) \ (MyBase.size() - n()))
            End If
        End Get
    End Property

    Public Shadows Function size() As Int64 Implements iParametersMapping.size
        'if cannot use serial_mapping logic, should return the size of vector, otherwise, maxInt64
        Return ifoper(MyBase.size() <= n(), MyBase.size(), maxInt64)
    End Function

    Public Overrides Function ToString() As String
        Dim rtn As String = Nothing
        rtn = MyBase.ToString()
        If Not isemptystring(rtn) AndAlso n() < maxInt64 Then
            rtn += "{" + Convert.ToString(n()) + "}"
        End If
        Return rtn
    End Function
End Class

Public Class empty_parameters_mapping
    Inherits parameters_mapping(Of _maxInt64)

    Private Shared _empty As iParametersMapping = Nothing

    Public Shared Function empty() As iParametersMapping
        Return _empty
    End Function

    Shared Sub New()
        _empty = New empty_parameters_mapping()
    End Sub

    Private Sub New()
        MyBase.New(New vector(Of String)())
    End Sub
End Class
